<?php
/**
 * Created by PhpStorm.
 * User: tangyunfei
 * Date: 2018/11/14
 * Time: 10:14
 */
class IndexAction extends  CommonsAction {




    function index(){

            import('ORG.Util.Page');// 导入分页类
           $cityinfo = M('City')->where("pinyin='".$this->domain."'")->find();
           if(empty($cityinfo)){
               //地区
               $this->areaid = M('Area')->where("pinyin='".$this->domain."'")->getField("id");
           }
            if (!empty($this->cityid)){
                if ($this->cityid == 370){
                    $count      =  M('Zhaopin')->where('status=1')->count();// 查询满足要求的总记录数
                    $Page       = new Page($count,150);// 实例化分页类 传入总记录数和每页显示的记录数
                    $show       = $Page->show();// 分页显示输出
                    $temp = M('Zhaopin')->where("status=1 AND top=0")->limit($Page->firstRow.','.$Page->listRows)->order('top=2 DESC,createtime desc')->select();
                }else{
                    $count      =  M('Zhaopin')->where('status=1 AND cityid='.$this->cityid)->count();// 查询满足要求的总记录数
                    $Page       = new Page($count,150);// 实例化分页类 传入总记录数和每页显示的记录数
                    $show       = $Page->show();// 分页显示输出
                    $temp = M('Zhaopin')->where("status=1 AND top=0  AND cityid=".$this->cityid)->limit($Page->firstRow.','.$Page->listRows)->order('top=2 DESC,createtime desc')->select();
                }
            }else{


                $count      =  M('Zhaopin')->where('status=1 AND areaid='.$this->areaid)->count();// 查询满足要求的总记录数
                $Page       = new Page($count,150);// 实例化分页类 传入总记录数和每页显示的记录数
                $show       = $Page->show();// 分页显示输出
                $temp = M('Zhaopin')->where("status=1 AND top=0  AND areaid=".$this->areaid)->limit($Page->firstRow.','.$Page->listRows)->order('top=2 DESC,createtime desc')->select();
            }

        //推广
        $viptemp = M('Zhaopin')->where(array('vip'=>1,'viptime'=>array('gt',time())))->order("viporder ASC")->select();
        foreach ($viptemp as $v){
            $cate =M('Category')->where("id=".$v['cid'])->find();

            $v['city'] = M('City')->where("id=".$v['cityid'])->getField("pinyin");

            $v['catename'] = $cate['name'];

            $v['catepath'] = $cate['path'];
            $vipinfo[] = $v;

        }
        $this->assign("vipinfo",$vipinfo);

        //十万火急
            $param['top'] = array('in','4');
            $param['toptime'] = array('gt',time());
            $huojitemp1 = M('Zhaopin')->where($param)->order("createtime desc")->select();
            $param['top'] = array('in','3');
            $param['toptime'] = array('gt',time());
            $param['cityid'] = $this->cityid;
            $huojitemp2 = M('Zhaopin')->where($param)->order("createtime desc")->select();
            if(empty($huojitemp1)){
                $huoji1 = array();
            }else{
                foreach ($huojitemp1 as $v){
                    $v['road'] =M('Category')->where("id=".$v['cid'])->getField("path");
                    $v['city']= M('City')->where("id=".$v['cityid'])->getField('pinyin');
                    $huoji1[]=$v;
                }
            }
            if(empty($huojitemp2)){
                $huoji2 = array();
            }else{
                foreach ($huojitemp2 as $v){
                    $v['road'] =M('Category')->where("id=".$v['cid'])->getField("path");
                    $v['city']= M('City')->where("id=".$v['cityid'])->getField('pinyin');
                    $huoji2[]=$v;
                }
            }
            $huoji = array_merge($huoji1,$huoji2);




            //城市置顶
            $topinfo1 = M('Zhaopin')->where("status=1 AND  cityid=".$this->cityid." AND top=1 AND toptime>".mktime())->order("createtime desc")->select();
            //全局置顶
            $topinfo2 = M('Zhaopin')->where("status=1 AND top=2 AND toptime>".mktime())->order("createtime desc")->select();

            $cityadlist = M('ad')->where('cityid='.$this->cityid)->select();   // 得到以城市分类的广告

            $cityname = M('city')->where('id='.$this->cityid)->getField('city');
            $citypinyin = M('city')->where('id='.$this->cityid)->getField('pinyin');
            if(!isset($cityname)){
                $cityname = M('city')->where('id='.$areainfo['id'])->getField('name');
            }
            if(!empty($temp)){
                $arrr = "";
                if(!empty($topinfo2)){
                    $arrr = $topinfo2;
                }
                if(!empty($topinfo1)){
                    $arrr = array_merge($arrr,$topinfo1);
                }

                if(!empty($arrr)){
                    $temp = array_merge($arrr,$temp);
                }



            }else{
                $arrr = "";
                if(!empty($topinfo2)){
                    $arrr = $topinfo2;
                }
                if(!empty($topinfo1)){
                    $arrr = array_merge($arrr,$topinfo1);
                }
                $temp = $arrr;
            }
            foreach ($temp as $v){
                $v['cate'] =M('Category')->where("id=".$v['cid'])->getField("name");
                $v['road'] =M('Category')->where("id=".$v['cid'])->getField("path");
                $v['city']= M('City')->where("id=".$v['cityid'])->getField('pinyin');
                $v['createtime']= date("m-d",$v['createtime']);
                $list[]=$v;
            }

            $this->flash =M('Flash')->select();

            $temp1 =  M('Zhaopin')->where('status=1')->limit(100)->order("id DESC")->select();

            foreach ($temp1 as $v){
                $v['cate'] =M('Category')->where("id=".$v['cid'])->getField("name");
                $v['cityname']=M('City')->where('id='.$v['cityid'])->getField("pinyin");
                $v['road'] =M('Category')->where("id=".$v['cid'])->getField("path");
                $v['createtime']= date("Y-m-d",$v['createtime']);
                $qita[]=$v;
            }

            //手机端任务列表分页显示
            import('ORG.Util.Page');// 导入分页类
            if (!empty($this->cityid)){
                if ($this->cityid == 370){
                    $count_m      =  M('Zhaopin')->where('status=1')->count();// 查询满足要求的总记录数
                    $Page_m       = new Page($count_m,150);// 实例化分页类 传入总记录数和每页显示的记录数
                    $show_m       = $Page_m->show();// 分页显示输出
                    $temp_m = M('Zhaopin')->where("status=1 AND top=0")->limit($Page_m->firstRow.','.$Page_m->listRows)->order('top=2 DESC,createtime desc')->select();
                }else{
                    $count_m      =  M('Zhaopin')->where('status=1 AND cityid='.$this->cityid)->count();// 查询满足要求的总记录数
                    $Page_m      = new Page($count_m,150);// 实例化分页类 传入总记录数和每页显示的记录数
                    $show_m       = $Page_m->show();// 分页显示输出
                    $temp_m = M('Zhaopin')->where("status=1 AND top=0  AND cityid=".$this->cityid)->limit($Page_m->firstRow.','.$Page_m->listRows)->order('top=2 DESC,createtime desc')->select();
                }
            }else{
                $count_m      =  M('Zhaopin')->where('status=1 AND areaid='.$this->areaid)->count();// 查询满足要求的总记录数
                $Page_m       = new Page($count_m,150);// 实例化分页类 传入总记录数和每页显示的记录数
                $show_m       = $Page_m->show();// 分页显示输出
                $temp_m = M('Zhaopin')->where("status=1 AND top=0  AND areaid=".$this->areaid)->limit($Page_m->firstRow.','.$Page_m->listRows)->order('top=2 DESC,createtime desc')->select();
            }
            if(!empty($temp_m)){
                $arrr = "";
                if(!empty($topinfo2)){
                    $arrr = $topinfo2;
                }
                if(!empty($topinfo1)){
                    $arrr = array_merge($arrr,$topinfo1);
                }

                if(!empty($arrr)){
                    $temp_m = array_merge($arrr,$temp_m);
                }



            }else{
                $arrr = "";
                if(!empty($topinfo2)){
                    $arrr = $topinfo2;
                }
                if(!empty($topinfo1)){
                    $arrr = array_merge($arrr,$topinfo1);
                }
                $temp_m = $arrr;
            }
            foreach ($temp_m as $v){
                $v['cate'] =M('Category')->where("id=".$v['cid'])->getField("name");
                $v['road'] =M('Category')->where("id=".$v['cid'])->getField("path");
                $v['city']= M('City')->where("id=".$v['cityid'])->getField('pinyin');
                $v['createtime']= date("Y-m-d",$v['createtime']);
                $list_m[]=$v;

            }


            $Page1       = new Page(500,100);// 实例化分页类 传入总记录数和每页显示的记录数
            $show1       = $Page1->show();
            $temp_m1 =  M('Zhaopin')->where('status=1')->limit($Page1->firstRow.','.$Page1->listRows)->order("id DESC")->select();

            foreach ($temp1 as $v){
                $v['cate'] =M('Category')->where("id=".$v['cid'])->getField("name");
                $v['cityname']=M('City')->where('id='.$v['cityid'])->getField("pinyin");
                $v['road'] =M('Category')->where("id=".$v['cid'])->getField("path");
                $v['createtime']= date("Y-m-d",$v['createtime']);
                $qita_m[]=$v;
            }


            $this->company =M('Company')->where("cityid=".$this->cityid." AND category > 0")->order("id DESC")->limit(8)->select();


            $catelink = M('link')->where('status=1 AND cityid='.$this->cityid)->select();   //城市分类友情链接



            $tempcate = M('Category')->where('status=1')->select();	   //分类
            foreach ($tempcate as $k=>$v){
                $str= M('Category')->where('status=1 and id='.$v['id'])->getField('name');
                $x=count($tempcate)-1;
                if($k === $x){
                    $catelistx.= $str;
                }else{
                    $catelistx.=$str.'、';
                }

            }
            $catelist=cutStr($catelistx,27);   //截取分类字段

            $this->cityad = M('Ad')->where(array('type'=>'cityads','endtime'=>array('gt',time()),'cityid'=>array('in','0,'.$this->cityid.'')))->select();

            if (!empty($this->cityid)){
                $this->diqu = M('area')->where(array('cityid'=>$this->cityid))->select();
            }else{
                $ainfo = M('Area')->where("pinyin='".$this->domain."'")->find();
                $this->diqu = M('area')->where(array('cityid'=>$ainfo['cityid']))->select();
            }
            if(!isset($cityname)){
                $cityname = M('area')->where("pinyin='".$this->city."'")->getField('name');
                $areacityname = M('city')->where("id='".$this->areainfo['cityid']."'")->getField('city');
                $areacitypinyin = M('city')->where("id='".$this->areainfo['cityid']."'")->getField('pinyin');
                $this->assign("areacityname",$areacityname);
                $this->assign("areacitypinyin",$areacitypinyin);
            }

            // 推广 spread
            $spread = M('Zhaopin')->where("spread_sort <> 0 and spread_sort_time > '".date('Y-m-d H:i:s')."' ")->order('spread_sort +0 asc')->select();
            foreach ($spread as $u) {

                $cate = M('Category')->where("id=" . $u['cid'])->find();

                $u['city'] = M('City')->where("id=" . $u['cityid'])->getField("pinyin");

                $u['catename'] = $cate['name'];

                $u['catepath'] = $cate['path'];

                $spreadArr[] = $u;

            }
            $this->assign('spread', $spreadArr);

            $this->assign("cityadlist",$cityadlist);
            $this->assign("catelist",$catelist);
            $this->assign("list",$list);


            $this->assign("catelink",$catelink);
            //$this->assign("cityname",$cityname);
            $this->assign("cityname",$cityname);
            $this->assign("citypinyin",$citypinyin);
            $this->assign("qita",$qita);
            $this->assign("list_m",$list_m);
            $this->assign("qita_m",$qita_m);
            $this->assign('page',$show);
            $this->assign('page1',$show1);
            $this->assign("huoji",$huoji);


            $this->display();



    }

    function cate(){

        $this->display();
    }
}